﻿Public Class fGastos
    Dim daGatos As lbGlobalcom.dsCajaTableAdapters.CAJ_GASTOTableAdapter
    Dim TGas As lbGlobalcom.cTipoGasto
    Private Sub CargaTipoGasto()
        Try
            If IDTIPOGASTOTextBox.Text.Trim.Length > 0 Then
                TGas = New lbGlobalcom.cTipoGasto(IDTIPOGASTOTextBox.Text)
                txtDescTipoGas.Text = TGas.Descripcion
                TGas = Nothing
            Else
                txtDescTipoGas.Text = ""
            End If
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
    Private Sub btnBuscaTipoGasto_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscaTipoGasto.Click
        Try
            Dim fTipoG As New fListaTipoGastos
            If fTipoG.ShowDialog = Windows.Forms.DialogResult.OK Then
                IDTIPOGASTOTextBox.Text = fTipoG.idTipoGasto
            End If
            IDTIPOGASTOTextBox.Focus()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub fGastos_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            If lbGlobalcom.CustomPrincipal.EsCajero Then
                daGatos = New lbGlobalcom.dsCajaTableAdapters.CAJ_GASTOTableAdapter
                DsCaja.CAJ_GASTO.Columns("IDDOCUMENTO").DefaultValue = "GAS"
                DsCaja.CAJ_GASTO.Columns("FECHA").DefaultValue = Date.Today
                DsCaja.CAJ_GASTO.Columns("IDCAJA").DefaultValue = lbGlobalcom.CustomPrincipal.IdCaja.Trim
            Else
                MessageBox.Show("El usurio debe de estar configurado como cajero para poder ejecutar esta opción", "Error Usuario", MessageBoxButtons.OK, MessageBoxIcon.Stop)
            End If
            
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    Private Sub SalvaGasto(ByVal Msj As Boolean)
        Try
            CAJ_GASTOBindingSource.EndEdit()
            daGatos.Update(DsCaja.CAJ_GASTO)
            If Msj Then _
                MessageBox.Show("Se salvarón registros con exito", "Salvar Registros", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    Private Sub CAR_GASTOBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CAR_GASTOBindingNavigatorSaveItem.Click
        SalvaGasto(True)
    End Sub

    Private Sub BindingNavigatorDeleteItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorDeleteItem.Click
        If MessageBox.Show("Esta seguro que desea eliminar este registro", "Elimina registro", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
            CAJ_GASTOBindingSource.RemoveCurrent()
            SalvaGasto(False)
        End If
    End Sub

    Private Sub NUMEROTextBox_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles NUMEROTextBox.Validating
        Try
            If daGatos.GastoExiste(IDDOCUMENTOTextBox.Text, NUMEROTextBox.Text) = 1 Then
                If MessageBox.Show("Registro ya existe, desea cargarlo", "Registro Existe", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                    DsCaja.CAJ_GASTO.Rows.Clear()
                    daGatos.FillByIdDocNumero(DsCaja.CAJ_GASTO, IDDOCUMENTOTextBox.Text, NUMEROTextBox.Text)
                End If
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub BindingNavigatorAddNewItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorAddNewItem.Click
        Try
            CAJ_GASTOBindingSource.EndEdit()
            If DsCaja.CAJ_GASTO.GetChanges() IsNot Nothing Then
                If MessageBox.Show("Hay cambios pendientes por salvar, desea salvarlos", "Salvar Cambios", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                    SalvaGasto(False)
                End If
            End If
            DsCaja.CAJ_GASTO.Rows.Clear()
            CAJ_GASTOBindingSource.AddNew()
            NUMEROTextBox.Text = lbGlobalcom.cGeneral.GetConsecGastos()
            IDTIPOGASTOTextBox.Focus()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub IDTIPOGASTOTextBox_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles IDTIPOGASTOTextBox.Validating
        Try
            CargaTipoGasto()
        Catch ex As Exception
            txtDescTipoGas.Text = ""
            MessageBox.Show(ex.Message)
            e.Cancel = True
        End Try
    End Sub
    Private Sub CargaGasto(ByVal Tipo As String, ByVal Numero As Integer)
        If DsCaja.CAJ_GASTO.GetChanges() IsNot Nothing Then
            If MessageBox.Show("Hay cambios pendientes por salvar, desea salvarlos", "Salvar Cambios", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                SalvaGasto(False)
            End If
        End If
        DsCaja.CAJ_GASTO.Rows.Clear()
        daGatos.FillByIdDocNumero(DsCaja.CAJ_GASTO, Tipo, Numero)
        CargaTipoGasto()
    End Sub
    Private Sub btnBuscaGasto_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscaGasto.Click
        Try
            Dim flGas As New fListaGastos
            If flGas.ShowDialog = Windows.Forms.DialogResult.OK Then
                CargaGasto(flGas.mTipo, flGas.mNumero)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
End Class